package io.renren.modules.ccs.service.impl;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.ccs.dao.TypeDao;
import io.renren.modules.ccs.entity.RecordEntity;
import io.renren.modules.ccs.entity.TypeEntity;
import io.renren.modules.ccs.service.RecordService;
import io.renren.modules.ccs.service.TypeService;


@Service("typeService")
public class TypeServiceImpl extends ServiceImpl<TypeDao, TypeEntity> implements TypeService {
	@Autowired
	RecordService ccsRecordService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
    	String typeName = (String)params.get("typeName");
    	Page<TypeEntity> page = this.selectPage(
			new Query<TypeEntity>(params).getPage(),
			new EntityWrapper<TypeEntity>()
				.like(StringUtils.isNotBlank(typeName),"type_name", typeName)
				.or().like(StringUtils.isNotBlank(typeName),"type_desc", typeName)
				.addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
		);
    	for(TypeEntity type : page.getRecords()){
			Long count= (long) ccsRecordService.selectCount(new EntityWrapper<RecordEntity>().eq("record_type",type.getTypeId()));
			type.setRecordCount(count);
		}
        return new PageUtils(page);
    }

}
